package com.yunyu.demo.video;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.litalk.ffmpeg.LTBaseModel;
import com.litalk.ffmpeg.LTFFMuxer;
import com.litalk.ffmpeg.LTImageVideo;
import com.litalk.ffmpeg.LTImageVideo2;
import com.litalk.ffmpeg.LTMobileFFmpegModel;
import com.litalk.ffmpeg.LTPasterManager;
import com.litalk.ffmpeg.LTVideoCheck;
import com.litalk.ffmpeg.LTVideoConvertModel;
import com.litalk.ffmpeg.LTVideoDecoder;
import com.litalk.ffmpeg.LTVideoEditCode;
import com.litalk.ffmpeg.LTVideoEffertModel;
import com.litalk.ffmpeg.LTVideoExtractModel;
import com.litalk.ffmpeg.LTVideoMetadata;
import com.litalk.ffmpeg.LTVideoSpeedModel;
import com.litalk.ffmpeg.LTVideoTaskQueue;
import com.litalk.ffmpeg.LTWaterMaskModel;
import com.litalk.ffmpeg.libnative.VideoConvertNative;
import com.litalk.ffmpeg.model.UserLayerUiParam;
import com.litalk.ffmpeg.paster.LTGifPaster;
import com.litalk.ffmpeg.paster.LTImagePaster;
import com.litalk.ffmpeg.paster.LTPngPaster;
import com.litalk.ffmpeg.paster.LTVideoPaster;
import com.litalk.ffmpeg.util.CacheFileUtil;
import com.yunyu.camera.opengl.FilterConfig;
import com.yunyu.demo.R;
import com.zeusee.main.hyperlandmark.FileUtil;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.LinkedList;
import java.util.List;

public class VideoActivity extends AppCompatActivity {
    private final String TAG = VideoActivity.class.getSimpleName();

    private static final String CACHE_INPUTFILES = "/ffmpeg/inputFiles/";
    private static final String CACHE_WATERMASK = "/ffmpeg/outputWatermask/";
    private static final String CACHE_WATERMASK2 = CACHE_WATERMASK + "soft";
    private static final String CACHE_CONVERT = "/ffmpeg/outputConvert/";
    private static final String CACHE_VIDEOEFFERT = "/ffmpeg/outputVideoEffert/";
    private static final String CACHE_VIDEOCONCAT = "/ffmpeg/outputVideoConcat/";
    private static final String CACHE_KEYFRAME = "/ffmpeg/outputKeyFrames/";
    private static final String CACHE_VIDEOCUT = "/ffmpeg/outputVideoCut/";
    private static final String CACHE_VIDEOSPEED = "/ffmpeg/outputVideoSpeed/";
    private static final String CACHE_VIDEOCMD = "/ffmpeg/outputVideoCmd/";
    private static final String CACHE_DIR = "/ffmpeg/cache/";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.frag_video);

        //创建所有目录
        createCacheOutputDir(CACHE_INPUTFILES);
        createCacheOutputDir(CACHE_WATERMASK);
        createCacheOutputDir(CACHE_WATERMASK2);
        createCacheOutputDir(CACHE_CONVERT);
        createCacheOutputDir(CACHE_VIDEOEFFERT);
        createCacheOutputDir(CACHE_VIDEOCONCAT);
        createCacheOutputDir(CACHE_KEYFRAME);
        createCacheOutputDir(CACHE_VIDEOCUT);
        createCacheOutputDir(CACHE_VIDEOSPEED);
        createCacheOutputDir(CACHE_VIDEOCMD);

        findViewById(R.id.btn_copy).setOnClickListener(v -> {
            //拷贝测试资源
            String retStr = FileUtil.copyFilesFromAssets(this, "videoTestFiles", getApplicationContext().getCacheDir() + CACHE_INPUTFILES);
            if (retStr != null)
                Toast.makeText(getApplicationContext(), "资源拷贝成功", Toast.LENGTH_SHORT).show();
        });

        findViewById(R.id.btn_video_watermark1).setOnClickListener(v -> {
            Toast.makeText(getApplicationContext(), "开始测试", Toast.LENGTH_LONG).show();
            //硬水印
            testUtilHardwareOverlay("otherType.mp4");

            //软水印
//            testUtilSoftOverlay("otherType.mp4");

            //硬转码
//            testUtilHardwareEncode("otherType.mp4");

            //硬特效
//            testUtilHardwareVideoEffert("otherType.mp4");
//
//        //拼接测试
//        testUtilVideoConcat("output1.mp4", "output2.mp4", "testConcat.mp4");
//
//        //关键帧提取
//        testUtilVideoSelect("h26460.mp4", 23, 41);
//
//        //时长编辑
//        testUtilVideoCut("testcut.mp4", 20, 60);
//
//        //视频变速
//        //0.5 0.75 1.0 1.5 2.0
//        testUtilVideoSpeed("videoTestFiles/otherType.mp4", 2.0f);
//
//        //视频预览帧抽取
//        testUtilVideoExtract("videoTestFiles/otherType.mp4");
//
//        //添加背景音乐 音量范围:0~100
//        testUtilAudioMixOnVideoFile("otherType -space.mp4", 50, "Dream_It_Possible.flac", 10);
//
//        //替换音轨
//        testUtilAudioReplaceOnVideoFile("videoTestFiles/otherType.mp4", "Dream_It_Possible.flac");
//
//        //删除音轨
//        testUtilDisableAudioTrackOnVideoFile("videoTestFiles/otherType.mp4");
//
//        //修改视频文件音量
//        testUtilModifyVolumeOnVideoFile("videoTestFiles/otherType.mp4", 50);
//
//        //读取metadata信息
//        testUtilReadVideoFileMetadata("otherType-metadata.mp4");
//
//        //测试拍同款
//        testUtilRefAudioOnVideoFile("kuaishou.mp4", "videoTestFiles/otherType.mp4");
//
//        //测试旋转适配(元数据旋转)
//        testUtilVideoRotate("videoTestFiles/otherType.mp4");
//
//        //测试音频文件变速
//        // 0.5 0.75 1.0 1.5 2.0
//        testUtilAudioFileSpeed("2222.mp3", 2.0f);
//
//        //测试合拍播放器
//        testUtilLTVideoDecoder("videoTestFiles/otherType.mp4");
//
//        //命令行测试
//        //(mp3 aac m4a wav amr flac)
//        testUtilCmd("1111.wav", "otherType1.aac");
//
//        //图片人脸识别
//        testUtilImageFaceDetector("testFace.jpg");
//
//        //视频录制测试
//        testUtilLTMuxer();
//
//        //影集录制测试
//        testSvgaRecord();
//
//        //视频特效测试
//        testVideoOpenGLFilter();
//
//        //测试读取手机信息
//        testReadMobileInfo();
//
//        //测试pag
//        testLibPag();
        });


    }

    /**
     * 硬件编码
     */
    private void testUtilHardwareEncode(String srcVideoName) {
        Log.d(TAG, "testUtilHardwareEncode: start");

        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilHardwareEncode" + srcVideoName);
            return;
        }
        File dstVideo = getCacheFile(CACHE_CONVERT, srcVideoName);

        if (!LTVideoCheck.getInstance().isVideoConvertSupport(srcVideo.getAbsolutePath())) {
            Log.e(TAG, "testUtilHardwareEncode: isVideoConvertSupport false");
            return;
        }

        LTVideoConvertModel videoConvertModel = new LTVideoConvertModel(srcVideo.getAbsolutePath(), dstVideo.getAbsolutePath());
//        videoConvertModel.setCustomSize(540, 940);
        videoConvertModel.setTaskCallback(new LTBaseModel.LTTaskCallback() {
            @Override
            public void onTaskDone() {
                Log.d(TAG, "onVideoConverDone: ");
            }

            @Override
            public void onTaskError(int err) {
                Log.e(TAG, "onVideoConvertError: " + err);
            }
        });

        LTVideoTaskQueue.getInstance().pushVideoTask(videoConvertModel);
    }

    /**
     * 硬水印
     */
    private void testUtilHardwareOverlay(String srcVideoName) {
        Log.d(TAG, "testUtilHardwareOverlay: start");
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilHardwareOverlay :" + srcVideoName);
            return;
        }
        File dstVideo = getCacheFile(CACHE_WATERMASK, srcVideoName);

        if (!LTVideoCheck.getInstance().isVideoConvertSupport(srcVideo.getAbsolutePath())) {
            Log.e(TAG, "testUtilHardwareOverlay: isVideoConvertSupport false");
            return;
        }

        //设置水印
        File overlayPng4 = getCacheFile(CACHE_INPUTFILES, "water_mark_1080.png");
        LTWaterMaskModel waterMaskModel = new LTWaterMaskModel(getApplicationContext(), srcVideo.getAbsolutePath(), overlayPng4.getAbsolutePath(), dstVideo.getAbsolutePath());
//        waterMaskModel.setCutOffTime(10,20);
//        waterMaskModel.softEncodeFirst();
        waterMaskModel.setTaskCallback(new LTBaseModel.LTTaskCallback() {
            @Override
            public void onTaskDone() {
                Log.d(TAG, "onWaterMaskDone: ");
            }

            @Override
            public void onTaskError(int err) {
                Log.e(TAG, "onWaterMaskError: " + err);
            }
        });

        //添加任务
        LTVideoTaskQueue.getInstance().pushVideoTask(waterMaskModel);
    }

    /**
     * 软水印
     */
    private void testUtilSoftOverlay(String srcVideoName) {
        Log.d(TAG, "testUtilHardwareOverlay: start");
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilHardwareOverlay :" + srcVideoName);
            return;
        }
        File dstVideo = getCacheFile(CACHE_WATERMASK2, srcVideoName);

        if (!LTVideoCheck.getInstance().isVideoConvertSupport(srcVideo.getAbsolutePath())) {
            Log.e(TAG, "testUtilHardwareOverlay: isVideoConvertSupport false");
            return;
        }

        //设置水印
        File overlayPng4 = getCacheFile(CACHE_INPUTFILES, "water_mark_1080.png");
        LTWaterMaskModel waterMaskModel = new LTWaterMaskModel(getApplicationContext(), srcVideo.getAbsolutePath(), overlayPng4.getAbsolutePath(), dstVideo.getAbsolutePath());
        //强制软编
        waterMaskModel.softEncodeFirst();

        waterMaskModel.setTaskCallback(new LTBaseModel.LTTaskCallback() {
            @Override
            public void onTaskDone() {
                Log.d(TAG, "onWaterMaskDone: ");
            }

            @Override
            public void onTaskError(int err) {
                Log.e(TAG, "onWaterMaskError: " + err);
            }
        });

        //添加任务
        LTVideoTaskQueue.getInstance().pushVideoTask(waterMaskModel);
    }

//    private void testUtilHardwareVideoEffert(String srcVideoName) {
//        Log.d(TAG, "testUtilHardwareVideoEffert: start");
//        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
//        if (!srcVideo.exists()) {
//            Log.e(TAG, "no srcVideo in testUtilHardwareOverlay :" + srcVideoName);
//            return;
//        }
//        if (!LTVideoCheck.getInstance().isVideoConvertSupport(srcVideo.getAbsolutePath())) {
//            Log.e(TAG, "testUtilHardwareOverlay: isVideoConvertSupport false");
//            return;
//        }
//
//        File dstVideo = getCacheFile(CACHE_VIDEOEFFERT, srcVideoName);
//        LTVideoEffertModel videoSpecialEffert = new LTVideoEffertModel(getApplicationContext(), srcVideo.getAbsolutePath(), dstVideo.getAbsolutePath());
//        //设置裁剪时长
////        videoSpecialEffert.setCutOffTime(5,20);
//        videoSpecialEffert.setTaskCallback(new LTBaseModel.LTTaskCallback() {
//            @Override
//            public void onTaskDone() {
//                Log.d(TAG, "onSuccess: videoSpecialEffert");
//            }
//
//            @Override
//            public void onTaskError(int err) {
//                Log.e(TAG, "onError: videoSpecialEffert err = " + err);
//            }
//        });
//        LTPasterManager pasterManager = new LTPasterManager(getApplicationContext());
//
//        //测试svga
//        if (svgaPaster != null) {
//            videoSpecialEffert.setSvgaPaster(svgaPaster);
////            pasterManager.addSvgaPaster(svgaPaster);
//        }
//
//        //测试png贴纸
//        File testPngPasterFile = getCacheFile(CACHE_INPUTFILES, "test.png");//149x160
//        LTPngPaster pngPaster = new LTPngPaster(testPngPasterFile);
//        pngPaster.setCallback(new LTImagePaster.LTImageCallback() {
//            @Override
//            public void onError(int err) {
//                Log.e(TAG, "pngPaster onError :" + err);
//            }
//        });
//        //填写UI布局信息
//        UserLayerUiParam userLayerUiParamPng = new UserLayerUiParam();
//        userLayerUiParamPng.videoPlayerWidth = 928;
//        userLayerUiParamPng.videoPlayerHeight = 480;
//        userLayerUiParamPng.pasterLayerRotate = 45;
//        userLayerUiParamPng.videoPlayerPasterLayerX = 0;
//        userLayerUiParamPng.videoPlayerPasterLayerY = 0;
//        userLayerUiParamPng.pasterLayerWidth = 218;
//        userLayerUiParamPng.pasterLayerHeight = 218;
//        userLayerUiParamPng.pasterLayerStartTime = 0.1f;
//        userLayerUiParamPng.pasterLayerEndTime = 5.0f;
//        pngPaster.userLayerUiParam = userLayerUiParamPng;
//        pasterManager.addPngPaster(pngPaster);
//
//        //测试gif贴纸
//        File testGifPasterFile = getCacheFile(CACHE_INPUTFILES, "test2.gif");//testGif test2
//        LTGifPaster gifPaster = new LTGifPaster(testGifPasterFile);
//        gifPaster.setCallback(new LTVideoPaster.LTVideoPasterCallback() {
//            @Override
//            public void onError(int err) {
//                Log.e(TAG, "gifPaster onError: " + err);
//            }
//        });
//        //填写UI布局信息
//        UserLayerUiParam userLayerUiParam = new UserLayerUiParam();
//        userLayerUiParam.videoPlayerWidth = 928;
//        userLayerUiParam.videoPlayerHeight = 480;
//        userLayerUiParam.pasterLayerRotate = 0;
//        userLayerUiParam.videoPlayerPasterLayerX = 0;//720/2
//        userLayerUiParam.videoPlayerPasterLayerY = 0;//372/2
//        userLayerUiParam.pasterLayerWidth = 200;
//        userLayerUiParam.pasterLayerHeight = 200;
//        userLayerUiParam.pasterLayerStartTime = 0.1f;
//        userLayerUiParam.pasterLayerEndTime = 5.0f;
//        //gif贴纸镜像
//        userLayerUiParam.hFlip = true;
//        gifPaster.userLayerUiParam = userLayerUiParam;
//        pasterManager.addGifPaster(gifPaster);
//
//        //关联贴纸管理器
//        videoSpecialEffert.setPasterManager(pasterManager);
//        //添加任务
//        LTVideoTaskQueue.getInstance().pushVideoTask(videoSpecialEffert);
//    }

    private void testUtilVideoConcat(String srcVideoName1, String srcVideoName2, String dstVideoName) {
        Log.i(TAG, "testUtilVideoConcat: start`" + dstVideoName);
        File srcVideo1 = getCacheFile(CACHE_INPUTFILES, srcVideoName1);
        if (!srcVideo1.exists()) {
            Log.e(TAG, "no srcVideo in testUtilVideoConcat :" + srcVideoName1);
            return;
        }
        File srcVideo2 = getCacheFile(CACHE_INPUTFILES, srcVideoName2);
        if (!srcVideo2.exists()) {
            Log.e(TAG, "no srcVideo in testUtilVideoConcat :" + srcVideoName2);
            return;
        }
        File[] srcFiles = new File[2];
        srcFiles[0] = srcVideo1;
        srcFiles[1] = srcVideo2;

        File dstVideo = getCacheFile(CACHE_VIDEOCONCAT, dstVideoName);

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        int ret = mobileFFmpegModel.concatVideos(srcFiles, dstVideo);
        if (ret < 0) {
            Log.e(TAG, "testUtilVideoConcat: err = " + ret);
        }
        Log.i(TAG, "testUtilVideoConcat: done`" + dstVideoName);
    }

    private void testUtilVideoSelect(String srcVideoName, int outWidth, int outHeight) {
        Log.i(TAG, "testUtilVideoSelect: start`" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilVideoSelect :" + srcVideoName);
            return;
        }

        //清空结果
        CacheFileUtil.delete(getApplicationContext().getCacheDir() + CACHE_KEYFRAME);
        createCacheOutputDir(CACHE_KEYFRAME);

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        int ret = mobileFFmpegModel.extractKeyFrame(srcVideo, outWidth, outHeight, getApplication().getCacheDir() + CACHE_KEYFRAME);
        if (ret < 0) {
            Log.e(TAG, "testUtilVideoSelect: err = " + ret);
        }
        Log.i(TAG, "testUtilVideoSelect: done`" + srcVideoName);
    }

    private void testUtilVideoCut(String srcVideoName, int startTime, int endTime) {
        Log.i(TAG, "testUtilVideoCut: start`" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilVideoCut :" + srcVideoName);
            return;
        }

//        File dstVideo = getCacheFile(CACHE_VIDEOCUT,srcVideoName);
        File dstVideo = getCacheFile(CACHE_VIDEOCUT, srcVideoName);

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        int ret = mobileFFmpegModel.cutVideo(srcVideo, startTime, endTime, dstVideo);
        if (ret < 0) {
            Log.e(TAG, "testUtilVideoCut: err = " + ret);
        }
        Log.i(TAG, "testUtilVideoCut: done`" + srcVideoName);
    }

    private void testUtilVideoSpeed(String srcVideoName, float speed) {
        Log.i(TAG, "testUtilVideoSpeed: start`" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilVideoSpeed :" + srcVideoName);
            return;
        }

        File dstVideo = getCacheFile(CACHE_VIDEOSPEED, srcVideoName);

        LTVideoSpeedModel videoSpeedModel = new LTVideoSpeedModel();
        int ret = videoSpeedModel.videoSpeed(srcVideo, dstVideo, speed);
        if (ret < 0) {
            Log.e(TAG, "testUtilVideoSpeed: err = " + ret);
        }
        Log.i(TAG, "testUtilVideoSpeed: done`" + srcVideoName);
    }

    private void testUtilVideoExtract(String srcVideoName) {
        Log.i(TAG, "testUtilVideoExtract: start`" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilVideoExtract :" + srcVideoName);
            return;
        }

        LTVideoExtractModel videoExtractModel = new LTVideoExtractModel();
        LTVideoExtractModel.LTVideoExtractModelCallback videoExtractModelCallback = new LTVideoExtractModel.LTVideoExtractModelCallback() {
            @Override
            public void onFrameCount(int count) {
                Log.d(TAG, "onFrameCount: " + count);
            }

            @Override
            public void onFrame(Bitmap bitmap, long pts) {
                Log.d(TAG, String.format("testUtilVideoExtract onFrame: %d", pts));
            }

            @Override
            public void onFrameEof() {
                Log.d(TAG, "onFrameEof: ");
            }

            @Override
            public void onFrameError() {
                Log.d(TAG, "onFrameError: ");
            }
        };
        videoExtractModel.extractVideoKeyFrame(srcVideo, 40, videoExtractModelCallback);
        Log.i(TAG, "testUtilVideoExtract: done`" + srcVideoName);
    }

    private void testUtilAudioMixOnVideoFile(String srcVideoName, int videoVolPer, String srcAudioName, int audioVolPer) {
        Log.i(TAG, "testUtilAudioMixOnVideoFile: initint..." + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        File srcAudio = getCacheFile(CACHE_INPUTFILES, srcAudioName);
        if (!srcVideo.exists() || !srcAudio.exists()) {
            Log.e(TAG, "no srcVideo or srcAudio in testUtilAudioMixOnVideoFile :" + srcVideoName);
            return;
        }
        File dstVideo = getCacheFile(CACHE_VIDEOEFFERT, srcVideoName);
        File cacheAudio = getCacheFile(CACHE_DIR, "tmp.aac");

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        //预处理音频文件
        int ret = mobileFFmpegModel.audioMixInitOnVideoFile(srcAudio, cacheAudio, 60);
        if (ret < 0) {
            Log.e(TAG, "testUtilAudioMixOnVideoFile: err = " + ret);
            return;
        }
        Log.i(TAG, "testUtilAudioMixOnVideoFile: start");
        //混音
        ret = mobileFFmpegModel.audioMixOnVideoFile(srcVideo, videoVolPer, cacheAudio, audioVolPer, dstVideo, "local", "000", "litalk");
        if (ret < 0) {
            Log.e(TAG, "testUtilAudioMixOnVideoFile: err = " + ret);
        }
        Log.i(TAG, "testUtilAudioMixOnVideoFile: done`" + srcVideoName);
    }

    private void testUtilAudioReplaceOnVideoFile(String srcVideoName, String srcAudioName) {
        Log.i(TAG, "testUtilAudioReplaceOnVideoFile: initing... `" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        File srcAudio = getCacheFile(CACHE_INPUTFILES, srcAudioName);
        if (!srcVideo.exists() || !srcAudio.exists()) {
            Log.e(TAG, "no srcVideo or srcAudio in testUtilAudioReplaceOnVideoFile :" + srcVideoName);
            return;
        }
        File dstVideo = getCacheFile(CACHE_VIDEOEFFERT, srcVideoName);
        File cacheAudio = getCacheFile(CACHE_DIR, "tmp.aac");

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        //预处理音频文件
        int ret = mobileFFmpegModel.audioReplaceInitOnVideoFile(srcAudio, cacheAudio, 60);
        if (ret < 0) {
            Log.e(TAG, "testUtilAudioReplaceOnVideoFile: err = " + ret);
            return;
        }
        Log.i(TAG, "testUtilAudioReplaceOnVideoFile: start");
        //替换音轨
        ret = mobileFFmpegModel.audioReplaceOnVideoFile(srcVideo, cacheAudio, dstVideo, "local", "000", "litalk", false);
        if (ret < 0) {
            Log.e(TAG, "testUtilAudioReplaceOnVideoFile: err = " + ret);
        }
        Log.i(TAG, "testUtilAudioReplaceOnVideoFile: done`" + srcVideoName);
    }

    private void testUtilDisableAudioTrackOnVideoFile(String srcVideoName) {
        Log.i(TAG, "testUtilDisableAudioTrackOnVideoFile: start" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        File dstVideo = getCacheFile(CACHE_VIDEOEFFERT, srcVideoName);

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        //预处理音频文件
        int ret = mobileFFmpegModel.disableAudioTrack(srcVideo, dstVideo);
        if (ret < 0) {
            Log.e(TAG, "testUtilDisableAudioTrackOnVideoFile: err = " + ret);
            return;
        }
        Log.i(TAG, "testUtilDisableAudioTrackOnVideoFile: done`" + srcVideoName);
    }

    private void testUtilModifyVolumeOnVideoFile(String srcVideoName, int volPer) {
        Log.i(TAG, "testUtilModifyVolumeOnVideoFile: start" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        File dstVideo = getCacheFile(CACHE_VIDEOEFFERT, srcVideoName);

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        //预处理音频文件
        int ret = mobileFFmpegModel.modifyVideoVolume(srcVideo, volPer, dstVideo, "local", "000", "litalk");
        if (ret < 0) {
            Log.e(TAG, "testUtilModifyVolumeOnVideoFile: err = " + ret);
            return;
        }
        Log.i(TAG, "testUtilModifyVolumeOnVideoFile: done`" + srcVideoName);
    }

    private void testUtilReadVideoFileMetadata(String srcVideoName) {
        Log.i(TAG, "testUtilReadVideoFileMetadata: start" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilReadVideoFileMetadata :" + srcVideoName);
            return;
        }
        LTVideoMetadata videoMetadata = new LTVideoMetadata();
        videoMetadata.readMetadataForAudioCreateInfo(srcVideo);

        Log.i(TAG, "testUtilReadVideoFileMetadata: done`" + srcVideoName);
    }

    private void testUtilRefAudioOnVideoFile(String refVideoName, String srcVideoName) {
        Log.i(TAG, "testUtilRefAudioOnVideoFile: start" + srcVideoName);
        File refVideo = getCacheFile(CACHE_INPUTFILES, refVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!refVideo.exists() || !srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilRefAudioOnVideoFile :" + srcVideoName);
            return;
        }
        File dstVideo = getCacheFile(CACHE_VIDEOEFFERT, srcVideoName);

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        int ret = mobileFFmpegModel.refAudioTrackOnVideoFile(srcVideo, refVideo, dstVideo, "local", "000", "litalk");
        if (ret < 0) {
            if (ret == LTVideoEditCode.ltError(LTVideoEditCode.LT_ERROR_REF_VIDEO)) {

            }
            Log.e(TAG, "testUtilRefAudioOnVideoFile: err = " + ret);
            return;
        }


        Log.i(TAG, "testUtilRefAudioOnVideoFile: done`" + srcVideoName);
    }

    private void testUtilVideoRotate(String srcVideoName) {
        Log.i(TAG, "testUtilVideoRotate: start" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilVideoRotate :" + srcVideoName);
            return;
        }
        File dstVideo = getCacheFile(CACHE_VIDEOEFFERT, srcVideoName);

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        int ret = mobileFFmpegModel.rotateVideoFile(srcVideo.getAbsolutePath(), 90, dstVideo.getAbsolutePath());
        if (ret < 0) {
            Log.e(TAG, "testUtilVideoRotate: err = " + ret);
            return;
        }
        Log.i(TAG, "testUtilVideoRotate: done`" + srcVideoName);
    }

    private void testUtilAudioFileSpeed(String srcAudioFileName, float speed) {
        Log.i(TAG, "testUtilAudioFileSpeed: start" + srcAudioFileName);
        File srcAudio = getCacheFile(CACHE_INPUTFILES, srcAudioFileName);
        if (!srcAudio.exists()) {
            Log.e(TAG, "no srcVideo in testUtilAudioFileSpeed :" + srcAudioFileName);
            return;
        }
        File dstAudio = getCacheFile(CACHE_VIDEOEFFERT, "test.aac");

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        int ret = mobileFFmpegModel.modifyAudioFileSpeed(srcAudio.getAbsolutePath(), speed, dstAudio.getAbsolutePath());
        if (ret < 0) {
            Log.e(TAG, "testUtilAudioFileSpeed: err = " + ret);
            return;
        }
        Log.i(TAG, "testUtilAudioFileSpeed: done`" + srcAudioFileName);
    }

    private void testUtilLTVideoDecoder(String srcVideoName) {
        Log.i(TAG, "testUtilLTVideoDecoder: start LTVideoDecoder" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilLTVideoDecoder :" + srcVideoName);
            return;
        }
        //getApplicationContext().getFilesDir()+otherType2
        LTVideoDecoder videoDecoder = new LTVideoDecoder(srcVideo.getAbsolutePath(), new LTVideoDecoder.LTVideoDecoderCallback() {
            @Override
            public void onBitmapRend(Bitmap bitmap, long pts) {
//                Log.d(TAG, String.format("LTVideoDecoder onBitmapRend: pts = %d",pts));
            }

            @Override
            public void onBufferRend(IntBuffer rgbaBuffer, long pts) {

            }

            @Override
            public void onDecoderEnd(int ret) {
                if (ret < 0) {
                    Log.e(TAG, String.format("testUtilLTVideoDecoder: LTVideoDecoder err = %d", ret));
                } else {
                    Log.i(TAG, "testUtilLTVideoDecoder: LTVideoDecoder done`" + srcVideoName);
                }
            }

            @Override
            public void onPreviewFrame(Bitmap bitmap, long pts) {
                Log.d(TAG, "onPreViewFrame: ");
            }

            @Override
            public void onAudioRecordBuffer(ByteBuffer audioBuffer, long pts) {

            }

            @Override
            public void onCPUDecoderSlow(long pts) {

            }

            @Override
            public void onHighFpsMode() {

            }

            @Override
            public void onError(int code) {

            }
        });
        videoDecoder.setScaleOutputSize(540, 960);
        videoDecoder.startDecoder();
    }


    private void testUtilLTVideoDecoderAlloc(String srcVideoName) {
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        LTVideoDecoder videoDecoder = new LTVideoDecoder(srcVideo.getAbsolutePath(), new LTVideoDecoder.LTVideoDecoderCallback() {
            @Override
            public void onBitmapRend(Bitmap bitmap, long pts) {
//                Log.d(TAG, String.format("LTVideoDecoder onBitmapRend: pts = %d",pts));
            }

            @Override
            public void onBufferRend(IntBuffer rgbaBuffer, long pts) {

            }

            @Override
            public void onDecoderEnd(int ret) {
                if (ret < 0) {
                    Log.e(TAG, String.format("testUtilLTVideoDecoder: LTVideoDecoder err = %d", ret));
                } else {
                    Log.i(TAG, "testUtilLTVideoDecoder: LTVideoDecoder done`");
                }
            }

            @Override
            public void onPreviewFrame(Bitmap bitmap, long pts) {
                Log.d(TAG, "onPreViewFrame: ");
            }

            @Override
            public void onAudioRecordBuffer(ByteBuffer audioBuffer, long pts) {

            }

            @Override
            public void onCPUDecoderSlow(long pts) {

            }

            @Override
            public void onHighFpsMode() {

            }

            @Override
            public void onError(int code) {

            }
        });
        videoDecoder.setScaleOutputSize(540, 960);
        videoDecoder.startDecoder();
//        try {
//            Thread.sleep(500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        videoDecoder.stopDecoder();
    }

    private void testUtilCmd(String srcVideoName, String dstVideoName) {
        Log.i(TAG, "testUtilCmd: start`" + srcVideoName);
        File srcVideo = getCacheFile(CACHE_INPUTFILES, srcVideoName);
        if (!srcVideo.exists()) {
            Log.e(TAG, "no srcVideo in testUtilCmd :" + srcVideoName);
            return;
        }

        File dstVideo = getCacheFile(CACHE_VIDEOCMD, dstVideoName);

//        String cmd = String.format("-y -i %s -filter_complex [0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a] -map [v] -map [a] %s",srcVideo.getAbsolutePath(),dstVideo.getAbsolutePath());
        String cmd = String.format("-y -i %s -ar 44100 -ac 2 -ab 64k -bsf:a aac_adtstoasc %s", srcVideo.getAbsolutePath(), dstVideo.getAbsolutePath());

        LTMobileFFmpegModel mobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
        int ret = mobileFFmpegModel.cmdHelper(cmd);
        if (ret < 0) {
            Log.e(TAG, "testUtilCmd: err = " + ret);
        }
        Log.i(TAG, "testUtilCmd: done`" + srcVideoName);
    }

    private void testUtilImageFaceDetector(String srcImageName) {
        Log.i(TAG, "testUtilImageFaceDetector: start`" + srcImageName);
        File srcImage = getCacheFile(CACHE_INPUTFILES, srcImageName);
        if (!srcImage.exists()) {
            Log.e(TAG, "no srcVideo in testUtilImageFaceDetector :" + srcImageName);
            return;
        }
//        LTFaceDetector ltFaceDetector = new LTFaceDetector();
//        Bitmap bitmap = ltFaceDetector.cropFace(srcImage,480,480);

        Log.i(TAG, "testUtilImageFaceDetector: done`" + srcImageName);
    }

    private void testUtilLTMuxer() {
        File dstAudio = getCacheFile(CACHE_VIDEOEFFERT, "test.mp4");
        LTFFMuxer ltffMuxer = new LTFFMuxer(dstAudio.getAbsolutePath(), true, false);
        ltffMuxer.setVideoInfo(30, 30, 1280, 720, 1200000);
        int ret = ltffMuxer.start();
        Log.d(TAG, "testUtilLTMuxer: " + ret);
    }

//    private void testSvgaRecord() {
//        File testSvga = new File(FileMange.INSTANCE.getCacheRootPath() + "demo.svga");
//
//        //模拟bitmap资源
//        Bitmap bitmap1 = BitmapFactory.decodeFile(FileMange.INSTANCE.getCacheRootPath() + "ren.jpg");
//        Bitmap bitmap2 = BitmapFactory.decodeFile(FileMange.INSTANCE.getCacheRootPath() + "ren1.jpg");
//        List<Bitmap> bitmapList = new LinkedList<>();
//        bitmapList.add(bitmap1);
//        bitmapList.add(bitmap2);
//
//        //修改音频缓存
////        String cacheAudioDir = getApplication().getCacheDir() + CACHE_SVGA_AUDIO;
////        File file = new File(cacheAudioDir);
////        if (!file.exists()) {
////            file.mkdirs();
////        }
////        SVGAVideoEntity.Companion.setLtAudioCacheDir(cacheAudioDir);
//
//        LTImageVideo imageVideo = new LTImageVideo(getApplicationContext());
//        //加载view
//        LinearLayout linearLayout = findViewById(R.id.svgaView);
//        linearLayout.removeAllViews();
//        linearLayout.addView(imageVideo.animationView);
//        //设置svga地址
//        imageVideo.setSvgaUrl(testSvga, 0, 0);
//        //设置替换图集合
//        imageVideo.setCustomImages(bitmapList);
//        //设置水印资源
//        File waterMaskFile = new File(FileMange.INSTANCE.getCacheRootPath() + "testData/water_mark_1080.png");
//        imageVideo.setWaterMaskFileUrl(waterMaskFile.getAbsolutePath());
//
//        LTImageVideo.LTImageVideoCallback encodeCallback = new LTImageVideo.LTImageVideoCallback() {
//            @Override
//            public void onVideoEncodeProgress(double percentage) {
//
//            }
//
//            @Override
//            public void onVideoEncodeDone(String filePath) {
//                Log.d(TAG, "onVideoEncodeDone: ");
//            }
//
//            @Override
//            public void onVideoError(int ret) {
//                Log.e(TAG, "onVideoError: " + ret);
//            }
//
//            @Override
//            public void onVideoPreviewBitmap(Bitmap preview) {
//
//            }
//        };
//        //解析svga
//        imageVideo.decodeSvgaFile(new LTImageVideo.SVGAParserCallback() {
//            @Override
//            public void onParserDone() {
//                //获取可自定义图数量
//                int c = imageVideo.getSvgaCustomImageCount();
//                Log.d(TAG, "onParserDone: svgaCustomImageCount = " + c);
//                //设置svga编码视频监听
//                imageVideo.setCallback(encodeCallback);
//                //设置输出文件地址并输出
//                File imagesMovieFile = getCacheFile(CACHE_VIDEOEFFERT, "imagesMovie.mp4");
//                imageVideo.encodeMp4out(imagesMovieFile);
//            }
//
//            @Override
//            public void onParserError() {
//                Log.e(TAG, "onParserError: ");
//            }
//        });
//    }
//
//    String pagAdioFilePath = null;
//    private void testLibPag(){
//        File file = new File(FileMange.INSTANCE.getCacheRootPath()+"test.pag");
//        if (!file.exists())
//            return;
//        File outFile = getCacheFile(CACHE_VIDEOEFFERT, "testPag.mp4");
//        File outFile_audio = getCacheFile(CACHE_VIDEOEFFERT, "testPag_audio.mp4");
//
//        //模拟bitmap资源
//        Bitmap bitmap1 = BitmapFactory.decodeFile(FileMange.INSTANCE.getCacheRootPath() + "ren.jpg");
//        Bitmap bitmap2 = BitmapFactory.decodeFile(FileMange.INSTANCE.getCacheRootPath() + "ren1.jpg");
//        List<Bitmap> bitmapList = new LinkedList<>();
//        bitmapList.add(bitmap1);
//        bitmapList.add(bitmap2);
//
//        LTImageVideo2 ltImageVideo2 = new LTImageVideo2(getApplicationContext());
//        ltImageVideo2.decodePagFile(file.getAbsolutePath());
//        ltImageVideo2.setCustomImages(bitmapList);
//        Log.d(TAG, "testLibPag: nunImages = " + ltImageVideo2.getPagCustomImagesCount());
//        ltImageVideo2.setCallback(new LTImageVideo2.LTImageVideo2Callback() {
//            @Override
//            public void onVideoEncodeProgress(double percentage) {
////                Log.d(TAG, "onVideoEncodeProgress: " + percentage);
//            }
//
//            @Override
//            public void onVideoEncodeDone(String filePath) {
//                Log.d(TAG, "onVideoEncodeDone: ");
//
//                if(pagAdioFilePath != null){
//                    LTMobileFFmpegModel ltMobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
//                    int ret = ltMobileFFmpegModel.audioReplaceOnVideoFile(outFile, new File(pagAdioFilePath), outFile_audio,
//                            "1", "123", "demo",false);
//                    if (ret >= 0) {
//                        Log.d(TAG, "onOpenGLFilterEnd: 替换声音成功");
//                    } else {
//                        Log.e(TAG, "onOpenGLFilterEnd: 替换声音失败");
//                    }
//                }
//            }
//
//            @Override
//            public void onVideoError(int ret) {
//                Log.d(TAG, "onVideoError: "+ret);
//            }
//
//            @Override
//            public void onVideoPreviewBitmap(Bitmap preview) {
//                Log.d(TAG, "onVideoPreviewBitmap: ");
//            }
//
//            @Override
//            public void onAudioFileExtract(String audioFilePath) {
//                Log.d(TAG, "onAudioFileExtract: "+audioFilePath);
//                pagAdioFilePath = audioFilePath;
//            }
//        });
//
//        ltImageVideo2.encodeMp4Out(outFile.getAbsolutePath());
//    }
//
//    private void testVideoOpenGLFilter() {
//        File openGLFilterFile = getCacheFile(CACHE_VIDEOEFFERT, "openglFilter.mp4");
//        List<FilterConfig> filterConfigs = new LinkedList<>();
//
//        //模拟特效Filter
////        addTestFilter(0,3000, new GPUImageRGBFilter(1.0f,0.0f,1.0f), filterConfigs);
////        addTestFilter(3000,6000, new GPUImageSoulOutFilter(), filterConfigs);
//        addTestFilter(0, 4000, new GPUImageScaleFilter(), filterConfigs);
////        addTestFilter(9000,12000, new GPUImageShakeFilter(), filterConfigs);
////        addTestFilter(12000,15000, new GPUImageShineWhiteFilter(), filterConfigs);
////        addTestFilter(15000,18000, new GPUImageVertigoFilter(), filterConfigs);
////        addTestFilter(18000,21000, new GPUImageGlitchFilter(), filterConfigs);
//
//        //模拟svga特效
////        addTestFilter(0,3000, new GPUImageSourceOverBlendFilter(), filterConfigs);
//
//        LTOpenGLHelper openGLHelper = new LTOpenGLHelper(FileMange.INSTANCE.getCacheRootPath() + "8888.mp4", openGLFilterFile.getAbsolutePath(), filterConfigs);
//        openGLHelper.setCallback(new LTOpenGLHelper.LTOpenGLHelperCallback() {
//            @Override
//            public void onOpenGLFilterEnd() {
//                Log.d(TAG, "onOpenGLFilterEnd: ");
//                openGLHelper.destroyFilterConfig();
//
//                //替换声音
//                File testOutFile = getCacheFile(CACHE_VIDEOEFFERT, "test.mp4");
//                LTMobileFFmpegModel ltMobileFFmpegModel = new LTMobileFFmpegModel(getApplicationContext());
//                int ret = ltMobileFFmpegModel.refAudioTrackOnVideoFile(openGLFilterFile, new File(FileMange.INSTANCE.getCacheRootPath() + "problem.mp4"), testOutFile,
//                        "1", "123", "demo");
//                if (ret >= 0) {
//                    Log.d(TAG, "onOpenGLFilterEnd: 替换声音成功");
//                } else {
//                    Log.e(TAG, "onOpenGLFilterEnd: 替换声音失败");
//                }
//
//            }
//        });
//
//        //模拟贴纸特效
//        openGLHelper.setPasterManager(createPasterManager());
//
//        openGLHelper.start();
//        Log.d(TAG, "onOpenGLFilterEnd: 开始特效制作");
//    }

    private void testReadMobileInfo() {
        //getprop
        String value1 = VideoConvertNative.ltReadMobileInfo("ro.product.model");//读取机型
        String value2 = VideoConvertNative.ltReadMobileInfo("ro.vold.serialno");//读取序号
        String value3 = VideoConvertNative.ltReadMobileInfo("ro.build.version.sdk");//读取sdk版本
        String value4 = VideoConvertNative.ltReadMobileInfo("ro.product.cpu.abi");//读取abi
//        "arm64-v8a"
        Log.d(TAG, "testReadMobileInfo: ");
    }

    public boolean createCacheOutputDir(String dir) {
        boolean ret = true;
        File file = new File(getApplicationContext().getCacheDir() + dir);
        if (!file.exists()) {
            ret = file.mkdirs();
        }
        return ret;
    }

    private File getCacheFile(String dir, String name) {
        File tmpDir = new File(getApplicationContext().getCacheDir() + dir);
        if (!tmpDir.exists())
            tmpDir.mkdirs();
        File file = new File(tmpDir.getAbsolutePath(), name);
        return file;
    }
}
